﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using ChangeHope.Common;
using System.Data;

namespace YXShop.TemplateAction
{
    public partial class LabelParse
    {
        /// <summary>
        /// 留言列表和表单
        /// </summary>
        /// <returns></returns>
        public string LeaveWrod()
        {
            string mystyle = this.Label_Inserted;
            string TempletLayout = this.GetParamValue("YX:TempletLayout");
            string WhetherPagination = this.GetParamValue("YX:WhetherPagination");//是否分页
            string ShowRows = this.GetParamValue("YX:ShowRows");
            string ShowColumns = this.GetParamValue("YX:ShowColumns");
            string Option = this.GetParamValue("YX:Option");//判断操作类型表单或列表
            string IsStoreLeaveWord = this.GetParamValue("YX:IsStoreLeaveWord");
            if (Option == "LeaveWrodInfo")
            {
                int rows = 1;
                //显示行
                if (!string.IsNullOrEmpty(ShowRows) && ChangeHope.Common.ValidateHelper.IsNumber(ShowRows))
                {
                    rows = Convert.ToInt32(ShowRows);
                }

                int columns = 1;
                //显示列
                if (!string.IsNullOrEmpty(ShowColumns) && ChangeHope.Common.ValidateHelper.IsNumber(ShowColumns))
                {
                    columns = Convert.ToInt32(ShowColumns);
                }

                int PageSize = rows * columns;
                string strWhere = " and isauditing=1";//查询条件语句 显示所有通过审核的
                if (IsStoreLeaveWord == "1")
                {
                    string storeId = ChangeHope.WebPage.PageRequest.GetString("StoreId");
                    if (ChangeHope.Common.ValidateHelper.IsNumber(storeId))
                    {
                        strWhere += " and storeid=" + storeId + "";
                    }
                }
                else if (IsStoreLeaveWord == "1")
                {
                    strWhere += " and storeid=0";
                }
                string OrderFields = "[order by] id desc";

                YXShop.BLL.Accessories.Leaveword bll = new YXShop.BLL.Accessories.Leaveword();
                ChangeHope.DataBase.DataByPage dataPage = bll.GetList(OrderFields, PageSize, strWhere);

                StringBuilder Shtml = new StringBuilder();
                int r = 1;
                if (dataPage.DataReader != null)
                {
                    while (dataPage.DataReader.Read())
                    {
                        string strContent = mystyle;
                        #region 循环列表替换
                        if (strContent.IndexOf("[#Leave_Name]") > -1)
                        {
                            if (dataPage.DataReader["username"].ToString() != string.Empty && dataPage.DataReader["username"].ToString() != null)
                            {
                                strContent = strContent.Replace("[#Leave_Name]", dataPage.DataReader["username"].ToString());
                            }
                            else
                            {
                                strContent = strContent.Replace("[#Leave_Name]", "");
                            }


                        }
                        if (strContent.IndexOf("[#Leave_Time]") > -1)
                        {
                            if (dataPage.DataReader["adddate"].ToString() != string.Empty && dataPage.DataReader["adddate"].ToString() != null)
                            {
                                strContent = strContent.Replace("[#Leave_Time]", dataPage.DataReader["adddate"].ToString());
                            }
                            else
                            {
                                strContent = strContent.Replace("[#Leave_Time]", "");
                            }
                        }
                        if (strContent.IndexOf("[#Leave_Title]") > -1)
                        {
                            if (dataPage.DataReader["title"].ToString() != string.Empty && dataPage.DataReader["title"].ToString() != null)
                            {
                                strContent = strContent.Replace("[#Leave_Title]", dataPage.DataReader["title"].ToString());
                            }
                            else
                            {
                                strContent = strContent.Replace("[#Leave_Title]", "");
                            }
                        }

                        if (strContent.IndexOf("[#Leave_Content]") > -1)
                        {
                            if (dataPage.DataReader["content"].ToString() != string.Empty && dataPage.DataReader["content"].ToString() != null)
                            {
                                strContent = strContent.Replace("[#Leave_Content]", dataPage.DataReader["content"].ToString().Replace("\n", ""));
                            }
                            else
                            {
                                strContent = strContent.Replace("[#Leave_Content]", "");
                            }
                        }

                        if (strContent.IndexOf("[#Leave_IP]") > -1)
                        {
                            if (dataPage.DataReader["ip"].ToString() != string.Empty && dataPage.DataReader["ip"].ToString() != null)
                            {
                                strContent = strContent.Replace("[#Leave_IP]", dataPage.DataReader["ip"].ToString());
                            }
                            else
                            {
                                strContent = strContent.Replace("[#Leave_IP]", "");
                            }
                        }

                        if (strContent.IndexOf("[#Leave_Email]") > -1)
                        {
                            if (dataPage.DataReader["email"].ToString() != string.Empty && dataPage.DataReader["email"].ToString() != null)
                            {
                                strContent = strContent.Replace("[#Leave_Email]", dataPage.DataReader["email"].ToString());
                            }
                            else
                            {
                                strContent = strContent.Replace("[#Leave_Email]", "");
                            }
                        }

                        if (strContent.IndexOf("[#Leave_QQ]") > -1)
                        {
                            if (dataPage.DataReader["qq"].ToString() != string.Empty && dataPage.DataReader["qq"].ToString() != null)
                            {
                                strContent = strContent.Replace("[#Leave_QQ]", dataPage.DataReader["qq"].ToString());
                            }
                            else
                            {
                                strContent = strContent.Replace("[#Leave_QQ]", "");
                            }
                        }

                        if (strContent.IndexOf("[#Leave_RevertContent]") > -1)
                        {
                            if (dataPage.DataReader["replycontent"].ToString() != string.Empty && dataPage.DataReader["replycontent"].ToString() != null)
                            {
                                strContent = strContent.Replace("[#Leave_RevertContent]", dataPage.DataReader["replycontent"].ToString().Replace("\n", ""));
                            }
                            else
                            {
                                strContent = strContent.Replace("[#Leave_RevertContent]", "");
                            }
                        }

                        if (strContent.IndexOf("[#Leave_Address]") > -1)
                        {
                            if (dataPage.DataReader["address"].ToString() != string.Empty && dataPage.DataReader["address"].ToString() != null)
                            {
                                strContent = strContent.Replace("[#Leave_Address]", dataPage.DataReader["address"].ToString().Replace("\n", ""));
                            }
                            else
                            {
                                strContent = strContent.Replace("[#Leave_Address]", "");
                            }
                        }

                        if (strContent.IndexOf("[#Leave_RevertTime]") > -1)
                        {
                            if (dataPage.DataReader["replydate"].ToString() != string.Empty && dataPage.DataReader["replydate"].ToString() != null)
                            {
                                strContent = strContent.Replace("[#Leave_RevertTime]", dataPage.DataReader["replydate"].ToString());
                            }
                            else
                            {
                                strContent = strContent.Replace("[#Leave_RevertTime]", "");
                            }
                        }

                        if (strContent.IndexOf("[#Leave_RevertTime]") > -1)
                        {
                            if (dataPage.DataReader["replydate"].ToString() != string.Empty && dataPage.DataReader["replydate"].ToString() != null)
                            {
                                strContent = strContent.Replace("[#Leave_RevertTime]", dataPage.DataReader["replydate"].ToString());
                            }
                            else
                            {
                                strContent = strContent.Replace("[#Leave_RevertTime]", "");
                            }
                        }

                        //分页样式
                        if (TempletLayout == "0")
                        {
                            Shtml.Append(strContent);
                        }
                        else
                        {
                            r++;
                            Shtml.Append(strContent);
                            if (columns % r == 0)
                            {
                                Shtml.Append("</tr><tr>");
                            }
                        }
                        #endregion
                    }
                }//循环结束

                if (WhetherPagination == "0")
                {
                    Shtml.Append(dataPage.ReceptionToolBar);

                }
                else if (WhetherPagination == "1")
                {
                    return "<table width='100%'>" + Shtml.ToString() + "</table>";
                }
                return Shtml.ToString();
            }
            //表单
            else if (Option == "LeaveWrodForm")
            {
                #region 留言表单
                string strFormContent = mystyle;
                //留言类别
                if (strFormContent.IndexOf("[#form_type]") > -1)
                {
                    StringBuilder strType = new StringBuilder();
                    strType.Append("<select id=\"selLW_Type\" onchange=\"getindex();\">\n");
                    strType.Append("<option value=\"1\">普通留言</option>\n");
                    strType.Append("<option value=\"2\">购物投诉</option>\n");
                    strType.Append("<option value=\"3\">询问求购</option>\n");
                    strType.Append("<option value=\"4\">售后咨询</option>\n");
                    strType.Append("<option value=\"5\">加盟合作</option>\n");
                    strType.Append("</select><input type=\"hidden\" value=\"1\" name=\"hfType\" />\n");
                    strFormContent = strFormContent.Replace("[#form_type]", strType.ToString());
                }

                //姓名
                if (strFormContent.IndexOf("[#form_name]") > -1)
                {
                    strFormContent = strFormContent.Replace("[#form_name]", "<input id=\"txtLW_UserName\"  name=\"txtLW_UserName\" /><span id=\"spname\" style=\"margin-left: 20px; COLOR: #666666\"></span> \n");
                }

                //联系电话
                if (strFormContent.IndexOf("[#form_tel]") > -1)
                {
                    strFormContent = strFormContent.Replace("[#form_tel]", "<input name=\"txtLW_Telephone\" id=\"txtLW_Telephone\" type=\"text\"/>\n");
                }

                //联系QQ
                if (strFormContent.IndexOf("[#form_qq]") > -1)
                {
                    strFormContent = strFormContent.Replace("[#form_qq]", "<input name=\"txtLW_QQ\" id=\"txtLW_QQ\" type=\"text\"/>\n");
                }


                //联系地址
                if (strFormContent.IndexOf("[#form_address]") > -1)
                {
                    strFormContent = strFormContent.Replace("[#form_address]", "<input name=\"txtLW_Address\" id=\"txtLW_Address\" type=\"text\"/>\n");
                }
                //联系MSN
                if (strFormContent.IndexOf("[#form_msn]") > -1)
                {
                    strFormContent = strFormContent.Replace("[#form_msn]", "<input name=\"txtLW_MSN\" id=\"txtLW_MSN\" type=\"text\"/>\n");
                }

                //联系Email
                if (strFormContent.IndexOf("[#form_email]") > -1)
                {
                    strFormContent = strFormContent.Replace("[#form_email]", "<input name=\"txtLW_Email\" id=\"txtLW_Email\" type=\"text\"/>\n");
                }

                //留言主题
                if (strFormContent.IndexOf("[#form_title]") > -1)
                {
                    string storeId = ChangeHope.WebPage.PageRequest.GetString("StoreId");
                    if (storeId == "")
                    {
                        storeId = "0";
                    }
                    strFormContent = strFormContent.Replace("[#form_title]", "<input id=\"txtLW_Title\" name=\"txtLW_Title\"  type=\"text\" /><input type=\"hidden\" value=\"" + storeId + "\" name=\"isStoreLeaveWord\" /><span id=\"sptitle\" style=\"margin-left: 20px; COLOR: #666666\"></span>\n");
                }

                //留言内容
                if (strFormContent.IndexOf("[#form_content]") > -1)
                {
                    strFormContent = strFormContent.Replace("[#form_content]", "<textarea id=\"txtLW_Content\" name=\"txtLW_Content\" rows=\"5\" cols=\"60\"></textarea><span id=\"spcontent\" style=\"margin-left: 20px; COLOR: #666666\"></span> \n");
                }

                //提交
                if (strFormContent.IndexOf("[#form_submit]") > -1)
                {
                    strFormContent = strFormContent.Replace("[#form_submit]", "<input id=\"txtLW_submit\" name=\"txtLW_submit\" type=\"submit\" value=\"提交留言\" />\n");
                }
                //取消
                if (strFormContent.IndexOf("[#form_cancel]") > -1)
                {
                    strFormContent = strFormContent.Replace("[#form_cancel]", "<input id=\"txtLW_cancel\" name=\"txtLW_cancel\" type=\"reset\" value=\"重新填写\" />\n");
                }
                return strFormContent;
                #endregion
            }

            else
            {
                return "无法找到标签";
            }
        }
    }
}


